﻿using AutoStar.Common;
using GalaSoft.MvvmLight.CommandWpf;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Input;

namespace AutoStar.ViewModel
{
    public class WindowXYZVM : BaseViewModel
    {
        public WindowXYZVM()
        {
            XYZInit();
            //LocationChanged = new RelayCommand(XYZInit);
            ButtonX = new RelayCommand(MoveX);
            ButtonY = new RelayCommand(MoveY);
            ButtonZ = new RelayCommand(MoveZ);
            ButtonAll = new RelayCommand(MoveAll);
        }
        /// <summary>
        /// 初始化
        /// </summary>
        public void Load()
        {
            LocationType = new ObservableCollection<string>() { "相对位置", "绝对位置" };
        }
        /// <summary>
        /// 根据选择的位置类型，设置相应的坐标范围
        /// </summary>
        public void XYZInit()
        {
            try
            {
                if (SelectedLocation.Equals("相对位置"))
                {
                    //TODO: 相对位置移动
                    IsAbsolute = false;
                    XMin = 0;
                    XMax = 150;
                    YMin = 0;
                    YMax = 150;
                    ZMin = 0;
                    ZMax = 100;
                }
                else
                {
                    //TODO: 绝对位置移动
                    IsAbsolute = true;
                    XMin = 0;
                    XMax = (int)Global.printer.maxX;
                    YMin = 0;
                    YMax = (int)Global.printer.maxY;
                    ZMin = 0;
                    ZMax = (int)Global.printer.maxZ;
                }
                //XValue = "0";
                //YValue = "0";
                //ZValue = "0";
                

            }
            catch (Exception ex)
            {

            }
        }
        public void MoveAll()
        {
            if (IsAbsolute)
                Global.printer.moveToAbsolute(float.Parse(XValue), float.Parse(YValue), float.Parse(ZValue));
            else
                Global.printer.moveToAbsolute(Global.printer.X + float.Parse(XValue),
                    Global.printer.Y + float.Parse(YValue),
                     Global.printer.Z + float.Parse(ZValue));
            CurrentPosition = new Point3D(float.Parse(XValue), float.Parse(YValue), float.Parse(ZValue));
        }
        public void MoveX()
        {
            if (IsAbsolute)
                Global.printer.moveToAbsolute(float.Parse(XValue), Global.printer.Y, Global.printer.Z);
            else
                Global.printer.moveToAbsolute(Global.printer.X + float.Parse(XValue), Global.printer.Y, Global.printer.Z);
            CurrentPosition = new Point3D(float.Parse(XValue), CurrentPosition.Y, CurrentPosition.Z);
        }
        public void MoveY()
        {
            if (IsAbsolute)
                Global.printer.moveToAbsolute(Global.printer.X, float.Parse(YValue), Global.printer.Z);
            else
                Global.printer.moveToAbsolute(Global.printer.X, Global.printer.Y + float.Parse(YValue), Global.printer.Z);
            CurrentPosition = new Point3D(CurrentPosition.X, float.Parse(YValue), CurrentPosition.Z);
        }
        public void MoveZ()
        {
            if (IsAbsolute)
                Global.printer.moveToAbsolute(Global.printer.X, Global.printer.Y, float.Parse(ZValue));
            else
                Global.printer.moveToAbsolute(Global.printer.X, Global.printer.Y, Global.printer.Z + float.Parse(ZValue));
            CurrentPosition = new Point3D(CurrentPosition.X, CurrentPosition.Y, float.Parse(ZValue));
        }
        public ICommand ButtonX { get; private set; }
        public ICommand ButtonY { get; private set; }
        public ICommand ButtonZ { get; private set; }
        public ICommand ButtonAll { get; private set; }
        public ICommand LocationChanged { get; private set; }
        private bool _IsAbsolute;
        public bool IsAbsolute
        {
            get => _IsAbsolute;
            set => Set(ref _IsAbsolute, value);
        }
        private string _XValue = "0";
        public string XValue
        {
            get => _XValue;
            set
            {
                if (value.Length == 0)
                    value = "0";
                if (int.Parse(value) >= XMin && int.Parse(value) <= _XMax)
                    Set(ref _XValue, int.Parse(value).ToString());
            }
        }
        private string _YValue = "0";
        public string YValue
        {
            get => _YValue;
            set
            {
                if (value.Length == 0)
                    value = "0";
                if (int.Parse(value) >= YMin && int.Parse(value) <= _YMax)
                    Set(ref _YValue, int.Parse(value).ToString());
            }
        }
        private string _ZValue = "0";
        public string ZValue
        {
            get => _ZValue;
            set => Set(ref _ZValue, value);
        }
        private ObservableCollection<string> _LocationType = new ObservableCollection<string>() { "相对位置", "绝对位置" };
        public ObservableCollection<string> LocationType
        {
            get => _LocationType;
            set => Set(ref _LocationType, value);
        }
        private string _SelectedLocation = "绝对位置";
        public string SelectedLocation
        {
            get => _SelectedLocation;
            set
            {
                if (value.Equals("相对位置"))
                {
                    IsAbsolute = false;
                    XMin = 0;
                    XMax = 150;
                    YMin = 0;
                    YMax = 150;
                    ZMin = 0;
                    ZMax = 100;
                    XValue = "0";
                    YValue = "0";
                    ZValue = "0";
                }                                                                                                                                                                                                                                                                                                                                                              
                else
                {
                    IsAbsolute = true;
                    XMin = 0;                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                      
                    XMax = (int)Global.printer.maxX;
                    YMin = 0;
                    YMax = (int)Global.printer.maxY;
                    ZMin = 0;
                    ZMax = (int)Global.printer.maxZ;
                    XValue = Global.UploadInfo.xLocation.ToString();
                    YValue = Global.UploadInfo.yLocation.ToString();
                    ZValue = Global.UploadInfo.zLocation.ToString();
                }
                Set(ref _SelectedLocation, value);
            }
        }
        private Point3D _currentPosition = new Point3D(0, 0, 0);
        public Point3D CurrentPosition
        {
            get => _currentPosition;
            set => Set(ref _currentPosition, value);
        }
        private int _XMax = 100;
        public int XMax
        {
            get => _XMax;
            set => Set(ref _XMax, value);
        }
        private int _XMin = 0;
        public int XMin
        {
            get => _XMin;
            set => Set(ref _XMin, value);
        }
        private int _YMax = 100;
        public int YMax
        {
            get => _YMax;
            set => Set(ref _YMax, value);
        }
        private int _YMin = 0;
        public int YMin
        {
            get => _YMin;
            set => Set(ref _YMin, value);
        }
        private int _ZMax = 100;
        public int ZMax
        {
            get => _ZMax;
            set => Set(ref _ZMax, value);
        }
        private int _ZMin = 0;
        public int ZMin
        {
            get => _ZMin;
            set => Set(ref _ZMin, value);
        }
    }
}
